<template>
	<view class="warp">
		<view class="userInfo" @click.capture="toUserInfo">
			<cloud-image width="150rpx" height="150rpx" v-if="hasLogin&&userInfo.avatar_file&&userInfo.avatar_file.url" :src="userInfo.avatar_file.url"></cloud-image>
			
			<view v-else class="defaultAvatarUrl">
				<uni-icons color="#ffffff" size="50" type="camera" />
			</view>
			
			<view class="logo-title">
				<text class="uer-name" v-if="hasLogin">{{userInfo.nickname||userInfo.username||userInfo.mobile}}</text>
				<text class="uer-name" v-else>{{$t('mine.notLogged')}}</text>
				<uni-tag :text="userGird" type="warning"></uni-tag>
			</view>
		</view>
		<!-- <view class="section">
			<view class="section-box" v-for="(item,i) in localUser" :key="i" @click="cUser(i)" :class="i==index?'active':''">
				<view class="s-title">成为{{item.text}}</view>
				<view class="s-price">￥{{item.price}}</view>
			</view>
		</view> -->
		<uni-card :title="localUser[index].text+'说明'">
			<rich-text :nodes="userContent"></rich-text>
		</uni-card>
		<view class="ubtn" @click="createOrder">成为{{localUser[index].text}}</view>
		<uni-pay ref="uniPay" :to-success-page="false" @success="onSuccess"></uni-pay>
	</view>
</template>

<script>
	import { mapState } from "pinia";
	import { useUserStore } from "@/pinia/user.js";
	// const user = uniCloud.importObject('two-plus-one')
	const db = uniCloud.database();
	export default {
		data() {
			return {
				localUser:[{
					text:"幸福使者",
					price:1314
				}],
				index:0,
				localGird:["普通会员","幸福使者","幸福大使","幸福天使","联创"]
			}
		},
		computed: {
			...mapState(useUserStore,['userInfo','hasLogin','sysSetting','updateUserInfo']),
			userGird(){
				let userIndex = this.userInfo.newLevel || 0
				return this.localGird[userIndex]
			},
			userContent(){
				let str=''
				if(this.index==0){
					str=this.sysSetting.userMatchmaker
				}
				if(this.index==1){
					str=this.sysSetting.userYueLaou
				}
				if(this.index==2){
					str=this.sysSetting.userJoint
				}
				return str
			}
		},
		methods: {
			cUser(index){
				console.log("data",index)
				this.index = index
			},
			async createOrder(){
				let value = this.localUser[this.index].price
				console.log(this.userInfo.newLevel,this.index)
				if(this.userInfo.newLevel > 0){
					let content =  `您已经是幸福使者,无需再次支付`
					uni.showModal({
						title: '提示',
						content,
						showCancel:false
					})
					return
				}
				
				//有可能读取不到客户的_id,或者登录超时
				if(!this.userInfo._id){
					return uni.showModal({
						title:"提交失败",
						content:"支付失败，请重新登录后，重试",
						success(res) {
							if(res.confirm){
								uni.navigateTo({
									url:"uni_modules/uni-id-pages/pages/login/login-withoutpwd"
								})
							}
						}
					})
				}
				
				let now = new Date().getTime() //获取当前时间戳
				let code = '' + (parseInt(Math.random() * 1000000) + 1000000)
				code = code.substring(1, 7)
				let order_no = "RED" + code + now //业务订单号
				let type = this.localUser[this.index].text
				let obj = {
					order_no,//订单编号
					uid:this.userInfo._id, //购买人
					price:value*100,
					status:0,
					type,
				}
				let oRes = await db.collection('wolf-pay-order').add(obj)
				console.log('oRes',oRes)
				if(oRes.result.id){
					// let res = await user.setIdentity({id:this.userInfo._id,orderid:order_no})
					// console.log("res++",res.user)
					// this.updateUserInfo()
					let out_trade_no = `${order_no}-1`; // 生成插件支付单号
					let description = "成为红娘订单"
					// 打开支付收银台
					let payRes = await this.$refs.uniPay.open({
						total_fee: value*100, // 支付金额，单位分 100 = 1元
						order_no: order_no, // 业务系统订单号（即你自己业务系统的订单表的订单号）
						out_trade_no: out_trade_no, // 插件支付单号
						description, // 支付描述
						type: 'setIdentity', // 支付回调类型
						custom: obj, // 自定义数据
					});
					console.log("payRes",payRes)
				}
				// console.log("createOrder",this.userInfo._id,obj)
				// let res = await user.setIdentity({id:this.userInfo._id})
				// let res = await user.setProfitSharing({id:this.userInfo._id,orderid:"ceshi1111155554",price:30000})
				// console.log("res++",res)
			},
			// 监听事件 - 支付成功
			onSuccess(res) {
				console.log('success: ', res);
				if (res.user_order_success) {
					// 代表用户已付款，且你自己写的回调成功并正确执行了
					uni.showToast({
						title: "购买成功",
						icon: "none"
					})
					this.updateUserInfo()
					// uni.redirectTo({
					// 	url: `/uni_modules/uni-pay/pages/success/success?out_trade_no=${res.out_trade_no}&order_no=${res.pay_order.order_no}&pay_date=${res.pay_order.pay_date}&total_fee=${res.pay_order.total_fee}&adpid=${this.adpid}&return_url=${this.return_url}&main_color=${this.main_color}`
					// });
				} else {
					// 代表用户已付款，但你自己写的回调执行成功（通常是因为你的回调代码有问题）
			
				}
			},
		}
	}
</script>

<style scoped lang="scss">
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}
	.warp{
		flex: 1;
		flex-direction: column;
	}
	.userInfo {
		padding: 10px;
		padding-bottom: 30px;
		background-color: #dd356a;
		flex-direction: row;
		align-items: center;
		
	}
	.defaultAvatarUrl{
		width: 150rpx;
		height: 150rpx;
		background-color: #eceaea;
		border-radius: 100%;
		justify-content: center;
		align-items: center;
	}
	
	.logo-title {
		flex: 1;
		align-items: center;
		justify-content: space-between;
		flex-direction: row;
		margin-left: 10px;
	}
	
	.uer-name {
		height: 100rpx;
		line-height: 100rpx;
		font-size: 28rpx;
		color: #FFFFFF;
	}
	.section{
		flex-direction: row;
		padding: 10px;
		justify-content: space-around;
		margin: -10px 5px 0;
		background-color: #FFFFFF;
		border-radius: 5px 5px 0 0 ;
	}
	.section-box{
		border: 1px solid #fdf1f1;
		padding: 5px 10px;
		min-height: 100px;
		justify-content: space-around;
		border-radius: 3px;
		align-items: center;
	}
	.active{
		border: 1px solid #dd356a;
	}
	.s-title{
		font-weight: 700;
	}
	.s-price{
		font-size: 12px;
		color: #dd356a;
	}
	.ubtn{
		margin: auto;
		width: 150px;
		text-align: center;
		padding: 10px;
		// border: 1px solid #dd356a ;
		border-radius: 20px;
		background-color: #dd356a;
		color: #FFFFFF;
	}
</style>
